package tarea2test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainWindow extends JFrame implements Observer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MainModel model;
	
	private JTextField op1, op2;
	private JLabel calcLabel;
	private JPanel buttonPanel;
	private JPanel inputPanel;
	private JButton butSum, butRes, butMul, butDiv;
	
	public MainWindow(){
		super("Pseudo Calculadora");
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setModel(new MainModel(this));
		
		this.calcLabel = new JLabel(" ");
		this.calcLabel.setBackground(new Color(0, 0, 0));
		
		this.butSum = new JButton("+");
		this.butSum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainWindow.this.getModel().sum();
			}
		});
		
		this.butRes = new JButton("-");
		this.butRes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainWindow.this.getModel().res();
			}
		});
		
		this.butMul = new JButton("x");
		this.butMul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainWindow.this.getModel().mul();
			}
		});
		
		this.butDiv = new JButton("/");
		this.butDiv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					MainWindow.this.getModel().div();
				} catch (ArithmeticException e1) {
					JOptionPane.showMessageDialog(MainWindow.this, 
							"No se puede realizar una division por cero", "Error", 
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		this.buttonPanel = new JPanel(new GridLayout(3, 2, 7, 7));
		this.buttonPanel.add(this.butSum);
		this.buttonPanel.add(this.butRes);
		this.buttonPanel.add(this.butMul);
		this.buttonPanel.add(this.butDiv);
		
		this.op1 = new JTextField();
		this.op1.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent e) {
				MainWindow.this.getModel().setOp1(Float.parseFloat(MainWindow.this.op1.getText()));
			}
			
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		this.op2 = new JTextField();
		this.op2.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent e) {
				MainWindow.this.getModel().setOp2(Float.parseFloat(MainWindow.this.op2.getText()));
			}
			
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		this.inputPanel = new JPanel(new GridLayout(1, 2, 5, 5));
		this.inputPanel.add(this.op1);
		this.inputPanel.add(this.op2);
		
		this.setLayout(new BorderLayout());
		this.getContentPane().add(this.inputPanel, "North");
		this.getContentPane().add(this.calcLabel, "Center");
		this.getContentPane().add(this.buttonPanel, "South");
	
		this.pack();
		this.setVisible(true);
	}

	public void update(Observable o, Object arg) {
		this.calcLabel.setText(this.getModel().getResultString());
	}

	public MainModel getModel() {
		return this.model;
	}

	public void setModel(MainModel model) {
		this.model = model;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MainWindow();
	}

}
